package com.dropgame.tools;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;

public class SpriteButton {

	
	Texture button;
	
	public Sprite keyFrame;
	
	public Sprite one;
	public Sprite two;
	public Sprite three;
	public Sprite four;
	public Sprite five;
	
	public int bType;
	
	private InvertedInput input3;
	
	private Input input;
	
	private Boolean hasCamera = false;
	
	public Rectangle viewport;
	private OrthographicCamera guiCam;
	
	private boolean touched;
	boolean released;
	boolean toggle = true;
	boolean up = true;
	private boolean finished = false;
	
	private boolean touchable;
	
	public int xPos;
	public int yPos;
	
	public int touchX;
	public int touchY;
	
	int width;
	int height;
	
	float sinceTouched = 0.0f;
	
	float scale = 1.0f;
	
	public Rectangle bounds;
	
	public float speed;
	
	public SpriteButton(Sprite one, Sprite two, Sprite three, InvertedInput input, int xPos, int yPos, float speed){
		this.one = one;
		this.two = two;
		this.three = three;
		this.xPos = xPos;
		this.yPos = yPos;
		this.input3 = input;
		setTouched(false);
		this.speed = speed;
		keyFrame = one;
		bType = 3;
		setFinished(false);
		keyFrame.setPosition(xPos, yPos);
		this.bounds = keyFrame.getBoundingRectangle();
		
	
		
		
	}
	
	public SpriteButton(Sprite one, Sprite two, Sprite three, Input input, int xPos, int yPos, OrthographicCamera guiCam, Rectangle viewport, float speed){
		this.one = one;
		this.two = two;
		this.three = three;
		this.xPos = xPos;
		this.yPos = yPos;
		this.input = input;
		setTouched(false);
		this.speed = speed;
		keyFrame = one;
		bType = 3;
		setFinished(false);
		keyFrame.setPosition(xPos, yPos);
		this.bounds = keyFrame.getBoundingRectangle();
		hasCamera = true;
		this.viewport = viewport;
		this.guiCam = guiCam;

		
	
		
		
	}
	
	public SpriteButton(Sprite one, Sprite two, Sprite three, Sprite four, Sprite five, Input input, int xPos, int yPos, OrthographicCamera guiCam, Rectangle viewport, float speed){
		this.one = one;
		this.two = two;
		this.three = three;
		this.four = four;
		this.five = five;
		this.xPos = xPos;
		this.yPos = yPos;
		this.input = input;
		setTouched(false);
		bounds = one.getBoundingRectangle();
		this.speed = speed;
		keyFrame = one;
		bType = 5;
		setFinished(false);
		keyFrame.setPosition(xPos, yPos);
		this.bounds = keyFrame.getBoundingRectangle();
		hasCamera = true;
		this.viewport = viewport;
		this.guiCam = guiCam;
		
		
	}
	
	public SpriteButton(Sprite one, Sprite two, Sprite three, Sprite four, Sprite five, Input input, int xPos, int yPos, float speed){
		this.one = one;
		this.two = two;
		this.three = three;
		this.four = four;
		this.five = five;
		this.xPos = xPos;
		this.yPos = yPos;
		this.input = input;
		setTouched(false);
		bounds = one.getBoundingRectangle();
		this.speed = speed;
		keyFrame = one;
		keyFrame.setPosition(xPos, yPos);
		bType = 5;
		setFinished(false);
		this.bounds = keyFrame.getBoundingRectangle();
		
		
	}
	
	public Rectangle getBounds(){
		return bounds;
	}
	
	public void update(float deltaTime){
		
		if(hasCamera){	
			if(input.justTouched()){
				Vector3 worldCoordinates = new Vector3(input.getX(), input.getY(), 0);
				guiCam.unproject(worldCoordinates, viewport.x, viewport.y, viewport.width, viewport.height);
				if(bounds.contains(worldCoordinates.x, worldCoordinates.y)){
					setTouched(true);
				}
			}
		}
		
		else if(!(input3 == null)){
			if(bounds.contains(input3.getX(), input3.getY()) && input3.justTouched()){
				setTouched(true);
			}
		}
		
		
		keyFrame.setPosition(xPos, yPos);
		this.bounds = keyFrame.getBoundingRectangle();
		if(isTouched()) updateKeyFrame(deltaTime);
			
		}
	
	public void updateKeyFrame(float deltaTime){
		
		finished = false;
		
		if(toggle){
			if(bType == 3 && sinceTouched > speed*3) up = false;
			else if(bType == 5 && sinceTouched > speed*5) up = false;
		}
			
		if(up) sinceTouched += deltaTime;
		else sinceTouched -= deltaTime;
		
		if(sinceTouched < 0){ 
							sinceTouched = 0;
							up = true;
							setFinished(true);
							setTouched(false);}
		
		if(bType == 3){
			if(sinceTouched < speed){ keyFrame = one; keyFrame.setPosition(xPos, yPos); }  
			else if(speed < sinceTouched && sinceTouched < (speed*2)){ keyFrame = two; keyFrame.setPosition(xPos, yPos); }
			else if((speed*2) < sinceTouched){ 
											keyFrame = three; 
											keyFrame.setPosition(xPos, yPos);
											
											if(!toggle){
												setFinished(true);
												setTouched(false);
												sinceTouched = speed*3;}
											}
		}
		
		else if(bType == 5){
			if(sinceTouched < speed) {keyFrame = one; keyFrame.setPosition(xPos, yPos);}  
			else if(speed < sinceTouched && sinceTouched < (speed*2)) {keyFrame = two; keyFrame.setPosition(xPos, yPos);}
			else if((speed*2) < sinceTouched && sinceTouched < (speed*3)) {keyFrame = three; keyFrame.setPosition(xPos, yPos);}
			else if((speed*3) < sinceTouched && sinceTouched < (speed*4)) {keyFrame = four; keyFrame.setPosition(xPos, yPos);}
			else if((speed*4) < sinceTouched){ 
											keyFrame = five;
											keyFrame.setPosition(xPos, yPos);
											if(!toggle){ 
												setFinished(true);
												setTouched(false);
												sinceTouched = speed*5;}
											}
			
		}
		
		else{ keyFrame = one;
			  keyFrame.setPosition(xPos, yPos);}
			 
		
			 
	}

	public boolean isTouched() {
		return touched;
	}

	public void setTouched(boolean touched) {
		this.touched = touched;
		
	}
	
	public boolean isTouchable(){
		return touchable;
	}
	
	public void setTouchable(boolean touchable){
		this.touchable = touchable;
	}

	public boolean isFinished() {
		return finished;
	}

	public void setFinished(boolean finished) {
		this.finished = finished;
	}
	
	
}

